package com.chd.ledger_server.controller;

import com.chd.ledger_server.dao.ModelInstancePointsDao;
import com.chd.ledger_server.entity.ModelInstanceModels;
import com.chd.ledger_server.entity.ModelInstancePoints;
import com.chd.ledger_server.service.ModelInstanceModelsService;
import com.chd.ledger_server.domain.PageDto;
import com.chd.ledger_server.domain.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 实例模型表(ModelInstanceModels)表控制层
 * 包含有实点的部分接口
 *
 * @author makejava
 * @since 2025-07-18 19:54:18
 */
@RestController
@RequestMapping("modelInstanceModels")
@CrossOrigin
public class ModelInstanceModelsController {
    /**
     * 服务对象
     */
    @Resource
    private ModelInstanceModelsService modelInstanceModelsService;

    @Resource
    private ModelInstancePointsDao modelInstancePointsDao;

    /**
     * 分页查询
     *
     * @param pageDto 分页请求实体，仅包含ModelInstanceModels实体+分页数据
     * @return ModelInstanceModels列表
     */
    @PostMapping("/queryByPage")
    public Result queryByPage(@RequestBody PageDto pageDto) {
        PageInfo list = modelInstanceModelsService.queryAllModelInstanceModelssByPage(pageDto);
        if (list.getList().isEmpty()) return Result.buildFail("未查到相关信息！");
        return Result.buildSuccess(list);
    }

    /**
     * 根据实模名查询实模id
     * @param instanceName，实模名
     * @return instanceId
     */

    @GetMapping("/queryId")
    public Result queryId(String instanceName){
        long id = modelInstanceModelsService.queryId(instanceName);
        if(id < 0)return Result.buildFail("无此模型！");
        return Result.buildSuccess(id);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/queryById")
    public Result queryById(Integer id) {
        ModelInstanceModels entity = this.modelInstanceModelsService.queryById(id);
        if (entity == null) {
            return Result.buildFail("未找到记录");
        }
        return Result.buildSuccess(entity);
    }

    /**
     * 根据基点ID和实模ID查询设备ID
     * @param pointId 基点ID
     * @param instanceId 实模ID
     * @return 设备ID（deviceId）
     */
    @GetMapping("/queryDeviceId")
    public Result queryDeviceId(Integer pointId, Integer instanceId) {
        if (pointId == null || instanceId == null) {
            return Result.buildFail("基点ID和实模ID不能为空");
        }

        // 构造查询条件
        ModelInstancePoints queryPoint = new ModelInstancePoints();
        queryPoint.setPointId(pointId);
        queryPoint.setInstanceId(instanceId);

        // 查询符合条件的实例热点
        List<ModelInstancePoints> instancePoints = modelInstancePointsDao.queryAll(queryPoint);
        if (instancePoints.isEmpty()) {
            return Result.buildFail("未找到对应的实例热点记录");
        }

        // 返回第一个匹配的设备ID
        return Result.buildSuccess(instancePoints.get(0).getDeviceId());
    }

    /**
     * 新增数据
     *
     * @param modelInstanceModels 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    public Result add(@RequestBody ModelInstanceModels modelInstanceModels) {
        ModelInstanceModels result = this.modelInstanceModelsService.insert(modelInstanceModels);
        return Result.buildSuccess(result);
    }

    /**
     * 编辑数据
     *
     * @param modelInstanceModels 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody ModelInstanceModels modelInstanceModels) {
        ModelInstanceModels result = this.modelInstanceModelsService.update(modelInstanceModels);
        if (result == null) {
            return Result.buildFail("更新失败，记录不存在");
        }
        return Result.buildSuccess(result);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/deleteById")
    public Result deleteById(Integer id) {
        boolean success = this.modelInstanceModelsService.deleteById(id);
        if (success) {
            return Result.buildSuccess("删除成功");
        } else {
            return Result.buildFail("删除失败，记录不存在");
        }
    }
}
